package com.alibaba.fastjson.bvt;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.JacksonUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import junit.framework.TestCase;
import org.junit.Assert;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class PointTest2 extends TestCase {

    public void test_point() throws Exception {
        Point point = new Point(3, 4);
        String text = JSON.toJSONString(point);
        System.out.println(text);
        JsonNode jsonNode = (JsonNode) JSON.parse(text);
        System.out.println(jsonNode.findValue("x").asInt());
        System.out.println(jsonNode.findValue("y").asInt());
        Point point3 = JSON.parseObject(text, Point.class);
        Assert.assertEquals(point, point3);
    }

    public void test_point2() throws Exception {
        Point point = new Point(3, 4);
        JSON.parseObject(JSON.toJSONString(point));
        Point p1 = JSON.parseObject("{}", Point.class);
        List<Point> pointList = JSON.parseArray("[null,null]", Point.class);
        Assert.assertNull(JSON.parseObject("null", Point.class));
        Point p = JSON.parseObject("{\"@type\":\"java.awt.Point\"}", Point.class);
        VO vo = JSON.parseObject("{\"value\":null}", VO.class);
    }

    public void testparseArray1() throws Exception {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("ZHULI");
        arrayList.add("30");
        arrayList.add("ALI");
        String json = "{\"objects\" : [\"One\", \"Two\", \"Three\"]}";
        JsonNode node = JacksonUtil.getObjectMapper().readTree(json);
        List<JsonNode> jsonNodes = new ArrayList<>();
        jsonNodes.add(node);
//      JSONArray jsonArray =JSON.parseArray(JSON.toJSONString(arrayList));
        JSONArray jsonArray = new JSONArray(JsonNodeFactory.instance, jsonNodes);
        System.out.println(jsonArray.get(0));
        System.out.println(JSON.toJSONString(jsonArray));
        System.out.println(JSON.toJSONString(jsonArray));
        JSONArray jsonArray1 = JSON.parseArray(JSON.toJSONString(jsonArray));
        System.out.println("--------------------" + jsonArray1.get(0));
        System.out.println();
//      JSONArray jsonNodes1 =JSON.parseArray(JSON.toJSONString(jsonArray));
//      ArrayNode arrayNode=new ArrayNode(JsonNodeFactory.instance);
//      jsonArray=(JSONArray)arrayNode;


    }

    public static class VO {

        private Point value;

        public Point getValue() {
            return value;
        }

        public void setValue(Point value) {
            this.value = value;
        }

    }
}
